<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/3
 * Time: 11:23
 */
namespace app\index\controller;

use think\Db;
use think\Request;
use think\View;
use think\Controller;

class Zhucedizhi extends Controller
{
    //注册地址首页
    public function index()
    {
        $id=getProviderId();
        //省
        $province=Db::name('area')->where('level',0)->select();;
//        var_dump($province);die;
        $this->view->assign('province',$province);
        //市
        $city=Db::name('area')->where('level',1)->select();
        $this->view->assign('city',$city);
        $area=Db::name('area')->where('level',2)->select();
        $this->view->assign('area',$area);
        $data=Db::name('project')->where('status',1)->where(['projectType'=>1,'isSelled'=>0])->join('project_registeraddress','project.registerAddressId=project_registeraddress.registerAddressId')->field('projectId,projectName,project.registerAddressId,taxType,price,cityId,districtId,memberId,regAddressType,registerAddressFileId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit(10)->select();
        foreach ($data as $k=> $v){
            $data[$k]['price'] = detailPrice($v['price']);
            //项目图片
            $files=Db::name('files')->where('filesId',$v['registerAddressFileId'])->value('thumbFile');
            $data[$k]['files']=$files;
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;

        }
        $this->view->assign('data',$data);
        return view('registeraddresslist');

    }
    public function search()
    {
        $id=getProviderId();
        if(input('location')!="") {
            $where['districtId'] = input('location');
        }else{
            $where="";
        }
        if(input('search')!=""){
            $whe=input('search');
        }else{
            $whe="";
        }
        $data=Db::name('project')->where($where)->where('status',1)->where(['projectType'=>1,'isSelled'=>0])->join('project_registeraddress','project.registerAddressId=project_registeraddress.registerAddressId')->whereLike('projectName','%'.$whe.'%')->field('projectId,projectName,project.registerAddressId,taxType,price,cityId,districtId,memberId,regAddressType,registerAddressFileId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit(10)->select();
        foreach ($data as $k=> $v){
            $data[$k]['price'] = detailPrice($v['price']);
            //项目图片
            $files=Db::name('files')->where('filesId',$v['registerAddressFileId'])->value('thumbFile');
            $data[$k]['files']=$files;
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
        }
        return $data;

    }
    //注册地址详情页
    public function detail()
    {
        $id=$_GET['projectId'];
// 项目详情
        $project=Db::name('project')->where('projectId',$id)->join('project_registeraddress','project.registerAddressId=project_registeraddress.registerAddressId')->field('projectId,projectName,project.registerAddressId,taxType,price,cityId,districtId,businessScope,timeline,location,memberId,regAddressType,isSelled,registerAddressFileId')->find();
        $project['price'] = detailPrice($project['price']);
        // 项目相册
        $imgs=Db::name('files')->where('filesId',$project['registerAddressFileId'])->value('thumbFile');
        if($project['memberId']==0){
            $provider=getPlatform();
            $member=$provider['mobile'];
        }else{
            $member=Db::name('member')->where('memberId',$project['memberId'])->value('mobile');
        }
        $project['mobile']=$member;
        //地址使用期限
        $timeline=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','tl')->where('optionCode',$project['timeline'])->field('optionName')->find();
        $project['timeline']=$timeline['optionName'];
        //公司地址
        $cityName=Db::name('area')->where('areaId',$project['cityId'])->value('areaName');
        $district=Db::name('area')->where('areaId',$project['districtId'])->value('areaName');
        $project['areaName']=$cityName.$district;
        $project['areaN']=$cityName.$district.$project['location'];
        //公司税务类型
        $taxType=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','taxt')->where('optionCode',$project['taxType'])->field('optionName')->find();
        $project['taxType']=$taxType['optionName'];
        //公司经营范围
        $induType=Db::name('regaddress_industrytype')->where('registerAddressId',$project['registerAddressId'])->column('industryType');
        $types=implode(',',$induType);
        $wher['optionCode']=['in',$types];
        $inType=Db::name('menu')->join('option','menu.menuId=option.menuId')->where('menuCode','indt')->where($wher)->field('optionName')->select();
        $inTypes=array();
        foreach ($inType as $v1){
            $inTypes[]=$v1['optionName'];
        }
        $industryType=implode(' ',$inTypes);
        $project['industryType']=$industryType;
        $this->view->assign('imgs',$imgs);
        $this->view->assign('project', $project);
        return view('registeraddressinfo');
    }
    public function append()
    {
        $start=input('num')*10;
        $end=$start+10;
//        echo $start,$end;die;
        $id=getProviderId();
        $data=Db::name('project')->where('status',1)->where(['projectType'=>1,'isSelled'=>0])->join('project_registeraddress','project.registerAddressId=project_registeraddress.registerAddressId')->field('projectId,projectName,project.registerAddressId,taxType,price,cityId,districtId,memberId,regAddressType,registerAddressFileId')->order("case when memberId=".$id." then 1 ELSE 4 END", null)->limit($start,$end)->select();
        foreach ($data as $k=> $v){
            $data[$k]['price'] = detailPrice($v['price']);
            //项目图片
            $files=Db::name('files')->where('filesId',$v['registerAddressFileId'])->value('thumbFile');
            $data[$k]['files']=$files;
            //公司地址
            $cityName=Db::name('area')->where('areaId',$v['cityId'])->value('areaName');
            $district=Db::name('area')->where('areaId',$v['districtId'])->value('areaName');
            $data[$k]['areaName']=$cityName.$district;
        }
        return $data;
    }
}